<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if ( ! function_exists('fred')) {
    function fred($var, $die = FALSE) {
        if(ENVIRONMENT != 'production') {
            //echo '<div style="background-color:black; color:red">';
            echo '<pre>';
            print_r($var);
            echo '</pre>';
            //echo '</div>';

            if ($die)
                die();
        }
    }
}

if ( ! function_exists('db_save')) {
    function db_save($model) {
        if(ENVIRONMENT != 'development') {
            $where = array();
            foreach ($model->fields as $field)
                $where[$field] = $model->$field;

            unset($where['id']);
            if($model->where($where)->count() == 0) {
                if($model->save())
                    db_log();
                else
                    db_error($model);
            }
        }
    }
}

if ( ! function_exists('db_save_as_new')) {
    function db_save_as_new($model) {
        if(ENVIRONMENT != 'development') {
            if($model->save_as_new())
                db_log();
            else
                db_error($model);
        }
    }
}

if ( ! function_exists('db_update')) {
    function db_update($model, $where, $values) {
        if(ENVIRONMENT != 'development') {
            if($model->where($where)->update($values))
                db_log();
            else
                db_error($model);
        }
    }
}

if ( ! function_exists('db_delete_all')) {
    function db_delete_all($model, $where) {
        if(ENVIRONMENT != 'development') {
            $model->where($where)->get();
            if($model->delete_all())
                db_log();
            else
                db_error($model);
        }
    }
}

if ( ! function_exists('db_error')) {
    function db_error($errors) {
        if(ENVIRONMENT != 'production') {
            list($callee) = debug_backtrace();
            $arguments = $callee['args'];
            $total_arguments = count($arguments);
            ++$total_arguments;
            echo '<fieldset style="background: black; color: white; border:0.3em red solid;">';
            echo '<legend style="background:white; color: black; border: 0.1em red solid;padding:5px;">'.$callee['file'].' @ line: '.$callee['line'].'</legend>';
            echo '<pre>';
            $error_number = 1;

            foreach($errors->error->all as $error) {
                echo '<p style="color: red">Error #'.$error_number.'/'.$total_arguments.'</p>';
                echo $error;
                ++$error_number;
            }
            echo '</pre>';
            fred('<p style="color: red; font-size: 2em; font-weight: bold">DIE</p>');
            echo '</fieldset>';
        }
        die();
    }
}

if ( ! function_exists('db_log')) {
    function db_log() {
        if(ENVIRONMENT != 'production') {
            $CI = &get_instance();
            fred('<p style="padding-left: 2em; font-size: 0.8em; text-decoration: underline;">'.$CI->db->last_query().'</p>');
        }
    }
}